<!DOCTYPE html>
<html xmlns:th="https://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <title>修改密码</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <base href="/">
    <link rel="stylesheet" href="static/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="static/css/public.css" media="all">
    <style>
        .layui-form-item .layui-input-company {width: auto;padding-right: 10px;line-height: 38px;}
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <form id="addForm" class="layui-form layui-form-pane">
            <input type="hidden" id="managerId" name="managerId">
            <div class="layui-form-item">
                <label class="layui-form-label required">旧的密码</label>
                <div class="layui-input-block">
                    <input type="password" id="oldPassword" name="oldPassword" lay-verify="required" lay-reqtext="旧的密码不能为空" value="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">新的密码</label>
                <div class="layui-input-block">
                    <input type="password" id="newPassword" name="newPassword" lay-verify="required" lay-reqtext="新的密码不能为空" value="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">新的密码</label>
                <div class="layui-input-block">
                    <input type="password" id="againPassword" name="againPassword" lay-verify="required" lay-reqtext="新的密码不能为空"  value="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block" style="margin: 0;text-align: center">
                    <button class="layui-btn layui-btn-normal" style="width: 180px;" lay-submit lay-filter="saveBtn">确认保存</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script src="../static/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../static/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="../static/lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
<script th:inline="javascript">
    let data={
        managerId: null,
        newPassWord: null
    }

    $(function (){
        let manager=[[${manager}]];
        $('#managerId').val(manager.managerId);
        //提交表单
        $('#addForm').submit(function (e) {
            let newPassword=$('#newPassword').val();
            let againPassword=$('#againPassword').val();
            let oldPassWord=$('#oldPassword').val();
            $.ajax({
                type: "get",
                url: '/managerInfo/testPassWord',
                data: {oldPassWord: oldPassWord},
                success: function (res) {
                    if(res.code===200){
                        if (newPassword !== againPassword) {
                            layer.msg("两次密码输入不一致");
                            e.preventDefault();
                            return false;
                        }
                        data.managerId = $('#managerId').val();
                        data.newPassWord = newPassword;
                        $.ajax({
                            type: "post",
                            url: '/managerInfo/changePassWord',
                            data: data,
                            success: function (res) {
                                if (res.code === 200) {
                                    layer.confirm('当前身份已过期，请重新登录', {
                                        btn: ['确定'], //按钮
                                        icon:0,
                                        cancel: function (index, layero) {
                                            let load = layer.load(0, {shade: false}); //0代表加载的风格，支持0-2
                                            $.ajax({
                                                url:"/logout",
                                                type:"post",
                                                success:function(res){
                                                    if(res.code===200){
                                                        layer.msg(res.message, {time: 5000, icon:1,offset: [15]});
                                                        window.top.location.href = '/login';
                                                    }else{
                                                        layer.msg(res.message, {time: 5000, icon:2,offset: [15]});
                                                    }
                                                },
                                                error:function (error) {//请求失败后调用的函数
                                                    layer.msg(error, {time: 5000, icon:2,offset: [15]});
                                                },
                                                complete(){
                                                    layer.close(load);
                                                },
                                            });
                                        }
                                    },
                                     function(){
                                        let load = layer.load(0, {shade: false}); //0代表加载的风格，支持0-2
                                        $.ajax({
                                            url:"/logout",
                                            type:"post",
                                            success:function(res){
                                                if(res.code===200){
                                                    layer.msg(res.message, {time: 5000, icon:1,offset: [15]});
                                                    window.top.location.href = '/login';
                                                }else{
                                                    layer.msg(res.message, {time: 5000, icon:2,offset: [15]});
                                                }
                                            },
                                            error:function (error) {//请求失败后调用的函数
                                                layer.msg(error, {time: 5000, icon:2,offset: [15]});
                                            },
                                            complete(){
                                                layer.close(load);
                                            },
                                        });
                                    });
                                } else {
                                    layer.msg(res.message, {time: 2000, icon: 1, offset: [15]});
                                }
                            },
                            error: function (error) {
                                layer.msg(error, {time: 2000, icon: 2, offset: [15]});
                            }
                        })
                        e.preventDefault();
                    }else{
                        layer.msg(res.message, {time: 2000, icon: 2, offset: [15]});
                    }
                }, error: function (error) {
                    layer.msg(error, {time: 2000, icon: 2, offset: [15]});
                }
            });
            e.preventDefault();
        });
    });
</script>
</body>
</html>